package com.we823.cms.repository.service.beetl.tag;

import com.we823.cms.model.cms.Channel;
import com.we823.cms.repository.service.cms.ChannelService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("cms_channel")
@Scope("prototype")
public class ChannelTag extends AbstractTag {

	private @Resource ChannelService channelService;

	private static final String PARAM_ID = "id";

	@Override
	public void execute() {
		Integer id = getParam(PARAM_ID, 0);

		Channel channelModel = null;
		if (id > 0) {
			channelModel = channelService.findOne(id);

		} else {
			channelModel = new Channel();
			channelModel.setId(0);
			channelModel.setParent(null);
			channelModel.setName("一级栏目");
		}

		if (channelModel != null) {
			List<Channel> children = null;
			if (id > 0) {
				children = channelService.findByParent(channelModel, sort);
			} else {
				children = channelService.findByParent(null, sort);
			}
			channelModel.setChilds(children);
		}

		this.binds(channelModel);
	}
}
